// pages/distributionFunds/distributionFunds.js
var app = getApp();
var api = require("../../api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dates:'本月',
    payment:'',
    month:'',
    moneyList:'',
    page: 1,
    pageSize: 10,
    hasMoreData: true,
    According: false,
    minmoney:'',
    maxmoney:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.payment == 1) {
      this.setData({
        isShowCenter: true
      })
    }
    let date =new Date();
    let months = date.getFullYear() + '-' + ("0" + (date.getMonth() + 1)).slice(-2);
    this.setData({
      payment: options.payment,
      month: months
    })
  },
  bindDateChange: function (e) {
    let date = new Date();
    let months = date.getFullYear() + '-' + ("0" + (date.getMonth() + 1)).slice(-2);
    if (e.detail.value == months){
      this.setData({
        dates:'本月',
        month: e.detail.value
      })
    }else{
      this.setData({
        dates: e.detail.value,
        month: e.detail.value
      })
    }
    this.data.page = 1
    this.getList()
  },
  // 获取列表
  getList() {
    var that = this;
    app.request({
      url: api.distribution.commission_detail,
      method: 'post',
      data: {
        payment: that.data.payment,
        month: that.data.month,
        page: that.data.page
      },
      success: function (res) {
        var moneyLists = that.data.moneyList;
        if (res.code == 1) {
          wx.hideLoading();
          that.setData({
            minmoney: res.data.commission,
            maxmoney: res.data.total_commission
          })
          if (that.data.page == 1) {
            moneyLists = []
          }
          var moneyList = res.data.data;
          if (moneyList.length == 0) {
            that.setData({
              moneyList: moneyList,
              hasMoreData: false,
              According: true
            })
          } else if (moneyList.length < that.data.pageSize) {
            that.setData({
              moneyList: moneyLists.concat(moneyList),
              hasMoreData: false,
              According: false
            })
          } else {
            that.setData({
              moneyList: moneyLists.concat(moneyList),
              page: that.data.page + 1,
              hasMoreData: true,
              According: false
            })
          }
        } else {
          wx.showToast({
            title: '暂无数据',
            duration: 1000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading();
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.page = 1
    console.log(this.data.page)
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.getList()
    } else {
      wx.showToast({
        title: '没有更多数据',
        duration: 1000,
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})